package Leetcode.LeetcodeDemo1;

import java.util.HashSet;

class ListNode142 {
      int val;
    ListNode142 next;
      ListNode142(int x) {
          val = x;
          next = null;
      }
  }
public class LeetCode142 {

    public static ListNode142 detectCycle(ListNode142 head) {
        HashSet<Integer> hashSet=new HashSet<>();
        ListNode142 cur=head;
        while(cur!=null){
            boolean isOk=hashSet.add(cur.val);
            if(isOk==false){
                return cur;
            }
            cur=cur.next;
        }
        return null;

    }
}
